#include "talisman.h"
#include "Character\Character.h"
#include "CharacterFactory.h"
#include "Character\PlayerCharacter.h"
#include "qmessagebox.h"
#include "PlayerArea.h"
#include "Map\Map.h"
#include "Map\PositionKeeper.h"
#include "AdventureCardArea.h"
#include "qfiledialog.h"


/*Sets up the game board, including map, character and adventure card position, buttons. */
void Talisman::setup(void)
{
	QWidget *widget = new QWidget;
	setCentralWidget(widget);

	layout = new QHBoxLayout() ;

	layout->setMargin(6);

	try
	{
		mPlayer = CharacterFactory::makeCharacter("Monk.txt.cha"); //new PlayerCharacter("Programmer", "Programmer is a well-known coffee-addict whose main achievement of the year is quitting coffee altogether. In fact, it has been about 3 months since he had his last cup. As a result, he is very, very angry.", 8, 11);
	}
	catch (char * e)
	{
		QMessageBox msgBox;
		QString msg (e);
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		exit(1);
	}

	mArea = new PlayerArea(mPlayer);

	mPositionKeeper =new PositionKeeper();
	map = new talisman_map::Map(mPlayer, mPositionKeeper);
	layout->addWidget(map);

	mLoadMapButton = new QPushButton("Load Map...");
	mRollDie = new QPushButton("Roll the die!");
	mBackButton = new QPushButton("Go Counter-Clockwise");
	mForwardButton = new QPushButton("Go Clockwise");
	mBridgeButton = new QPushButton("Take the bridge");
	mPortalButton = new QPushButton("Use the protal");
	mLoadCharacterButton = new QPushButton("Load Character...");
	mEditMapButton = new QPushButton("Edit Map...");
	mEditCharacterButton = new QPushButton("Edit Character...");
	mUseAdventureCardButton = new QPushButton("Use Adv Card");

	leftPanel = new QWidget();
	QVBoxLayout *layout2 = new QVBoxLayout(leftPanel) ;


	QWidget *buttons = new QWidget();
	QWidget *diceAndCardArea = new QWidget();
	QGridLayout *layoutButtons = new QGridLayout(buttons);
	QHBoxLayout *layoutDiceAndCardArea  = new QHBoxLayout(diceAndCardArea);

	layoutButtons->addWidget(mLoadMapButton,1,0);
	layoutButtons->addWidget(mLoadCharacterButton,1,1);
	layoutButtons->addWidget(mEditMapButton,2,0);
	layoutButtons->addWidget(mEditCharacterButton,2,1);
	layoutButtons->addWidget(mForwardButton,3,0);
	layoutButtons->addWidget(mBackButton,3,1);
	layoutButtons->addWidget(mBridgeButton,4,0);
	layoutButtons->addWidget(mPortalButton,4,1);
	layoutButtons->addWidget(mRollDie,5,0,1,1);
	layoutButtons->addWidget(mUseAdventureCardButton, 5, 1, 1,1);

	mAdventureCardArea = new AdventureCardArea();	
	diceAndCardArea->setStyleSheet("QWidget { background-position: top; }");
	layoutDiceAndCardArea->addWidget(&die);
	layoutDiceAndCardArea->addWidget(mAdventureCardArea);
	layout2->addWidget(mArea);
	layout2->addWidget(buttons);

	layout2->addWidget(diceAndCardArea);

	connect(mLoadMapButton, SIGNAL(clicked()), this, SLOT(loadMap()));
	connect(mForwardButton, SIGNAL(clicked()), this, SLOT(goForward()));
	connect(mBackButton, SIGNAL(clicked()), this, SLOT(goBack()));
	connect(mBridgeButton, SIGNAL(clicked()), this, SLOT(takeBridge()));
	connect(mPortalButton, SIGNAL(clicked()), this, SLOT(usePortal()));
	connect(mRollDie, SIGNAL(clicked()), this, SLOT(rollDie()));
	connect(mLoadCharacterButton, SIGNAL(clicked()), this, SLOT(loadCharacter()));
	connect(mEditMapButton, SIGNAL(clicked()), this, SLOT(editMap()));
	connect(mEditCharacterButton, SIGNAL(clicked()), this, SLOT(editCharacter()));
	connect(mUseAdventureCardButton, SIGNAL(clicked()), this, SLOT(useAdvCard()));

	layout->addWidget(leftPanel);

	layout->setContentsMargins(0, 0, 0, 0);
	layout->setMargin(0);

	widget->setLayout(layout);

	QString message = tr("Choose the action from the Game menu on top!");

	setWindowTitle(tr("Talisman Game"));
	setMinimumSize(320, 320);
	resize(1270, 810);
}